<?php
namespace Home\Controller;
use Think\Controller;
use Home\Common\Smtp;
class LoginController extends Controller {
	
	
	// 登录首页
    public function index(){   
    	$this->assign('lang', LANG_SET);
    	$this->display();
    } 
    // 获取验证码
    public function get_verif_code() {
    	get_verify_code();
    }
    
    // 登录验证
    public function login() {
    	if(IS_AJAX) {
            $username = I('post.username');
            $password = I('post.password');
            $verif_code = I('post.verif_code');
            
            $res = array('flag'=>0, 'msg'=>'');
            
            // 判断验证码是否正确
            $bool = check_verify($verif_code);
            if ( $bool ) {
            	$member = D('member')->getAccountInfo($username);
            	if (! empty($member)) {
            		$password = enter_password($password);
            		if ($password == $member['password']) {
            			session('member', $member);
            			$res['flag'] = 1;
            		} else {
            			$res['flag'] = -3;
            			$res['msg'] = L('_username_password_error_');
            		}
            	} else {
            		$res['flag'] = -2;
            		$res['msg'] = L('_username_error_');
            	}
            } else {
            	$res['flag'] = -1;
            	$res['msg'] = L('_verification_code_error_');
            }
            
            echo json_encode($res);
    	}          		
    }




    public function logout() {
		session('member',null);
        header('location:' . U('Login/index'));   
    }
    
    public function forgot_password() {
    	if(IS_AJAX) {
    		$username = I('post.username');   		
    		$verif_code = I('post.verif_code');   	
    		$res = array('flag'=>0, 'msg'=>'');
    	
    		// 判断验证码是否正确
    		$bool = check_verify($verif_code);
    		if ( $bool ) {
    			$member = D('member')->getAccountInfo($username);
    			if (! empty($member)) {
    				$res['flag'] = 1;
    				$res['msg'] = L('_verification_success_');
    				$res['account'] = $member['account'];
    				$res['email'] = $member['email'];
    				$code = rand(1000,9999);
    				//给用户发送邮件并修改安全码
    				$title = "Racgrounp Password Edit";
    				$message = "Your security code is：".$code;
    				$fromemail = $member['email'];
    				$this->sendMail($title,$message,$fromemail);
    				
    				//修改邮件安全码
    				D('Member')->where(array('account'=>$member['account']))->save(array('safe_code'=>$code));
    			} else {
    				$res['flag'] = -2;
    				$res['msg'] = L('_username_error_');
    			}
    		} else {
    			$res['flag'] = -1;
    			$res['msg'] = L('_verification_code_error_');
    		}   		
    		echo json_encode($res);
    	}else{
    		$this->assign('lang', LANG_SET);
    		$this->display();
    	}
    }
    
     public function authentication() {   	
     	if(IS_AJAX) {
     		 $account = I('post.account');    		
     		 $safe_code = trim(I('post.safe_code'));
     		 if(empty($account)) {
     		 	header('location:' . U('Login/index'));
     		 }
     		 
     		 if(empty($safe_code)) {
     		 	header('location:' . U('Login/index'));
     		 }
     		 $member = D('member')->getAccountInfo($account);
     		 $res = array('flag'=>0, 'msg'=>'');
     		 if($member['safe_code'] == $safe_code){
     		 	$res['flag'] = 1;
     		 	$res['msg'] = L('_verification_success_');
     		 	$res['safe_code'] = $safe_code;
     		 	$res['account'] = $account;
     		 }else {
     		 	$res['flag'] = -1;
     		 	$res['msg'] = L('_safe_code_error_');
     		 }
     		 echo json_encode($res);
     	}else {
     		$account = I('get.account');
     		$email = I('get.email');
     		if(empty($account)) {
     			header('location:' . U('Login/index'));
     		}
     		
     		if(empty($email)) {
     			header('location:' . U('Login/index'));
     		}
     		
     		$member = D('member')->getAccountInfo($account);
     		if($member['email'] != $email) {
     			header('location:' . U('Login/index'));
     		}    		
     		$this->assign('email', $email);
     		$this->display();
     	}
     	
     }
    
     public function edit_password() {    
     	if(IS_AJAX) {     
     	    $account = trim(I('post.account'));
     	    $safe_code = trim(I('post.safe_code'));
     	    $password = I('post.password');
     	    $password_tow = trim(I('post.password_tow'));
     	    if(empty($account)) {
     	    	header('location:' . U('Login/index'));
     	    }    	    
     	    if(empty($safe_code)) {
     	    	header('location:' . U('Login/index'));
     	    }    	    
     	    if(empty($password)) {
     	    	header('location:' . U('Login/index'));
     	    }    	    
     	    if(empty($password_tow)) {
     	    	header('location:' . U('Login/index'));
     	    }
     	    $res = array('flag'=>0, 'msg'=>'');     	    
     	    if($password != $password_tow){
     	    	$res['flag'] = -1;
     	    	$res['msg'] = L('_password_two_not_consistent');
     	    }else {
     	    	$member = D('member')->getAccountInfo($account);
     	    	if($member['safe_code'] != $safe_code){
     	    		$res['flag'] = -2;
     	    		$res['msg'] = L('_unlawful_request_');
     	    	}else {
     	    		$save['password'] = enter_password($password);
     	    		$where['account'] = $account;
     	    		$ret = D('Member')->where($where)->save($save);
     	    		if($ret) {
     	    		    $res['flag'] = 1;
     	    			$res['msg'] = L('_edit_success_');
     	    		}
     	    	}
     	    }
     	    echo json_encode($res);   
     	}else{
     		$account = I('get.account');
     		$safe_code = I('get.safe_code');
     		if(empty($account)) {
     			header('location:' . U('Login/index'));
     		}     		 
     		if(empty($safe_code)) {
     			header('location:' . U('Login/index'));
     		}     		 
     		$member = D('member')->getAccountInfo($account);
     		if($member['safe_code'] != $safe_code) {
     			header('location:' . U('Login/index'));
     		}     		
     		$this->display();
     	}    	     	
     }
     
      public function sendMail($title, $message,$fromemail) {
     	vendor('PHPMailer.class#phpmailer'); //用vendor导入第三方邮件类
     	$mail = new \PHPMailer();
     	// 设置PHPMailer使用SMTP服务器发送Email
     	$mail->IsSMTP();
     	// 设置邮件的字符编码，若不指定，则为'UTF-8'
     	$mail->CharSet='UTF-8';
     	// 添加收件人地址，可以多次使用来添加多个收件人
     	$mail->AddAddress($fromemail);
     	// 设置邮件正文
     	$mail->Body=$message;
     	// 设置邮件头的From字段。
     	$mail->From= 'admin@racgroup.net';
     	// 设置发件人名字
     	$mail->FromName=  'Racgroup';
     	// 设置邮件标题
     	$mail->Subject = $title;
     	// 设置SMTP服务器。
     	$mail->Host= 'mail.racgroup.net';
     	// 设置为"需要验证"
     	$mail->SMTPAuth = true;
     	// 设置用户名和密码。
     	$mail->Username = 'admin@racgroup.net';;
     	$mail->Password = 'racgroup';// 这里是163邮箱的授权码 不是密码
     	// 发送邮件。
     	$mail->Send();
     	echo $mail->ErrorInfo;
     } 
       
}